package csc420groupproject;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class CustomJDialog extends JDialog implements ActionListener{
    public static final int ONLY_OK = 0;
    public static final int OK_AND_CANCEL = 1;
    public static final int ONLY_CANCEL = 2;
    public static final int YES_AND_NO = 3;
    public static final int YES_NO_AND_CANCEL = 4;
    public static final int ONLY_CLOSE = 5;

    private static CustomJDialog singleton = null;
    private static JLabel [] text = null;
    private static JButton [] buttons;
    private static Color ownerBG, ownerFG;

    private CustomJDialog(Frame owner, String title, boolean modal, String [] newText,
            int buttonOptions) {
        super (owner, title, modal);

        ownerFG = owner.getForeground();
        ownerBG = owner.getBackground();

        this.setBackground(ownerBG);
        this.setForeground(ownerFG);

        text = new JLabel [newText.length];
        buttons = initializeButtons(buttonOptions);

        JPanel interiorPanel = new JPanel();
        interiorPanel.setLayout(new BoxLayout(interiorPanel, BoxLayout.Y_AXIS));
        JPanel textPanel = new JPanel(new GridLayout(text.length, 1));
        JPanel buttonPanel = new JPanel(new GridLayout(1, buttons.length));

        for(int i = 0; i < ((text.length > buttons.length)? text.length
                : buttons.length); i++){
            if(i < text.length){
                if(newText[i] != null){
                    text[i] = new JLabel(newText[i]);
                    text[i].setForeground(ownerFG);
                    textPanel.add(text[i]);
                }
            }
            if(i < buttons.length){
                if(buttons[i] != null){
                    buttons[i].addActionListener(this);
                    buttonPanel.add(buttons[i]);
                }
            }
        }
        interiorPanel.add(textPanel);
        interiorPanel.add(buttonPanel);
        this.add(interiorPanel);
        this.setLocationRelativeTo(owner);
        this.setSize(new Dimension(interiorPanel.getWidth(), interiorPanel.getHeight()));
        pack();
    }

    private CustomJDialog(Frame owner, String title, boolean modal, Component c,
            int buttonOptions) {
        super (owner, title, modal);

        ownerFG = owner.getForeground();
        ownerBG = owner.getBackground();

        this.setBackground(ownerBG);
        this.setForeground(ownerFG);

        buttons = initializeButtons(buttonOptions);

        JPanel interiorPanel = new JPanel();
        interiorPanel.setLayout(new BoxLayout(interiorPanel, BoxLayout.Y_AXIS));
        JPanel buttonPanel = new JPanel(new GridLayout(1, buttons.length));

        for(int i = 0; i < buttons.length; i++){
            if(buttons[i] != null){
                buttons[i].addActionListener(this);
                buttonPanel.add(buttons[i]);
            }
        }
        interiorPanel.add(c);
        interiorPanel.add(buttonPanel);
        this.add(interiorPanel);
        this.setLocationRelativeTo(owner);
        this.setSize(new Dimension(interiorPanel.getWidth(), interiorPanel.getHeight()));
        pack();
    }

    //still a singleton...just creates a new singleton every time getInstance
    //is called
    public static CustomJDialog getInstance(Frame owner,
                                            String title,
                                            boolean modal,
                                            String [] newText,
                                            int buttonOptions) {
        //if(singleton != null){
        //    reinitializeJDialog(owner, title, modal,
        //            newText, newButtons);
        //    return singleton;
        //} else {
            return (singleton = new CustomJDialog(owner, title, modal,
                    newText, buttonOptions));
        //}
    }

    public static CustomJDialog getInstance(Frame owner,
                                            String title,
                                            boolean modal,
                                            Component c,
                                            int buttonOptions) {
        //if(singleton != null){
        //    reinitializeJDialog(owner, title, modal,
        //            newText, newButtons);
        //    return singleton;
        //} else {
            return (singleton = new CustomJDialog(owner, title, modal,
                    c, buttonOptions));
        //}
    }

    private static JButton[] initializeButtons(int buttonOptions) {
        JButton [] temp = null;

        if(buttonOptions == ONLY_OK){
            temp = new JButton[1];
            temp[0] = new JButton("OK");
        } else if(buttonOptions == OK_AND_CANCEL){
            temp = new JButton[2];
            temp[0] = new JButton("OK");
            temp[1] = new JButton("Cancel");
        } else if(buttonOptions == ONLY_CANCEL){
            temp = new JButton[1];
            temp[0] = new JButton("Cancel");
        } else  if(buttonOptions == YES_AND_NO){
            temp = new JButton[2];
            temp[0] = new JButton("Yes");
            temp[1] = new JButton("No");
        } else  if(buttonOptions == YES_NO_AND_CANCEL){
            temp = new JButton[3];
            temp[0] = new JButton("Yes");
            temp[1] = new JButton("No");
            temp[2] = new JButton("Cancel");
        }else if (buttonOptions == ONLY_CLOSE) {
            temp = new JButton[1];
            temp[0] = new JButton("Close");
        } else  {
            throw new IllegalArgumentException("Illegal buttons option: " +
                    buttonOptions);
        }

        for(JButton b: temp){
            b.setBackground(ownerBG);
            b.setForeground(ownerFG);
        }
        return temp;
    }

    //I'm trying to avoid this since the non static methods I'll need to access
    //can't be...or atleast I'm struggling to find a way.
    /*private void reinitializeJDialog(Frame owner, String title, boolean
            modal, String [] newText, String [] newButtons){
        setLocationRelativeTo(owner);
    }*/

    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();

        if(command.equals("OK")){
            this.setVisible(false);
        }else if(command.equals("Cancel")) {
            this.setVisible(false);
        }else if(command.equals("Close")) {
            this.setVisible(false);
        }
    }
}
